package admin

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"shangyuapi/data_source"
	"shangyuapi/models"
	"time"
)

type InvitecodesRBody struct {
	Data       []models.Invitecode `json:"data"`
	PageNo     int                 `json:"pageNo"`
	PageSize   int                 `json:"pageSize"`
	TotalCount int64               `json:"totalCount"`
	TotalPage  int64               `json:"totalPage"`
}
type InvitecodesBody struct {
	Status   int64 `json:"status"`
	PageNo   int   `json:"pageNo"`
	Pagesize int   `json:"pagesize"`
}

func Invitecodes(ctx *gin.Context) {
	request := InvitecodesBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Invitecode
	res := data_source.Db.Where("status != 3")
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	res.Model(&models.Invitecode{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := InvitecodesRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Invitecodeadd(ctx *gin.Context) {
	startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := startOfToday.Unix() - 28800

	// 获取今天的结束时间
	endOfToday := startOfToday.Add(24 * time.Hour).Add(-1 * time.Second)
	endTimestamp := endOfToday.Unix() - 28800

	rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
	vcode := fmt.Sprintf("%06v", rnd.Int31n(1000000))
	hnm := models.Invitecode{
		Status:  1,
		Inttime: startTimestamp,
		Endtime: endTimestamp,
		Code:    vcode,
	}
	data_source.Db.Create(&hnm)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type HongniangsRBody struct {
	Data       []models.Hongniang `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}
type HongniangsBody struct {
	Status   int64 `json:"status"`
	PageNo   int   `json:"pageNo"`
	Pagesize int   `json:"pagesize"`
	ID       int64 `json:"id"`
}

func Hongniangqyjy(ctx *gin.Context) {
	request := HongniangsBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	id := request.ID
	data_source.Db.Model(&models.Hongniang{}).Where("id = ?", id).Update("status", status)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Hongniangs(ctx *gin.Context) {
	request := InvitecodesBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Hongniang
	res := data_source.Db
	if status > 0 {
		res = res.Where("status = ?", status)
	}
	res.Model(&models.Hongniang{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := HongniangsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

type TixianlistRBody struct {
	Data       []models.Hongniangtixiansql `json:"data"`
	PageNo     int                         `json:"pageNo"`
	PageSize   int                         `json:"pageSize"`
	TotalCount int64                       `json:"totalCount"`
	TotalPage  int64                       `json:"totalPage"`
}

func Tixianlist(ctx *gin.Context) {
	request := HongniangsBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	currentPage := request.PageNo
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Hongniangtixiansql

	res := data_source.Db.Model(&models.Hongniangtixian{}).Select("hongniangtixian.*, hongniang.name as hnname, hongniang.phone as hnphone, bank.khh, bank.bankid, bank.name, bank.phone").Joins("left join hongniang on hongniang.id = hongniangtixian.hongniangid").Joins("left join bank on bank.id = hongniangtixian.yhkid")
	if status > 0 {
		res = res.Where("hongniangtixian.status = ?", status)
	}
	res.Model(&models.Hongniangtixian{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := TixianlistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return
}

func Tixiansh(ctx *gin.Context) {
	request := HongniangsBody{}
	ctx.ShouldBind(&request)
	status := request.Status
	id := request.ID
	data_source.Db.Model(&models.Hongniangtixian{}).Where("id = ?", id).Update("status", status)
	if status == 3 {
		var tixian models.Hongniangtixian
		data_source.Db.Where("id = ?", id).First(&tixian)

		var hongniang models.Hongniang
		data_source.Db.Where("hongniangid = ?", tixian.Hongniangid).First(&hongniang)

		hnpirce := models.Hongniangprice{
			Hongniangid: tixian.Hongniangid,
			State:       1,
			Addtime:     time.Now().Unix(),
			Title:       "红娘提现失败",
			Price:       tixian.Price,
			Houprice:    hongniang.Price + tixian.Price,
		}
		data_source.Db.Create(&hnpirce)
		hnupdate := models.Hongniang{
			Price:            hongniang.Price + tixian.Price,
			Tixianzhongprice: hongniang.Tixianzhongprice - tixian.Price,
		}
		data_source.Db.Where("id = ?", tixian.Hongniangid).Updates(&hnupdate)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
